package com.qdlc.p2p.dal.dao.impl;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;

import javax.persistence.Query;

import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Repository;

import com.qdlc.p2p.common.constant.Global;
import com.qdlc.p2p.common.exception.UserException;
import com.qdlc.p2p.common.util.StringUtil;
import com.qdlc.p2p.dal.common.dao.jpa.BaseDaoImpl;
import com.qdlc.p2p.dal.common.model.jpa.OrderFilter.OrderType;
import com.qdlc.p2p.dal.common.model.jpa.PageDataList;
import com.qdlc.p2p.dal.common.model.jpa.QueryParam;
import com.qdlc.p2p.dal.common.model.jpa.SearchFilter.Operators;
import com.qdlc.p2p.dal.dao.UserFreezeDao;
import com.qdlc.p2p.dal.dto.User;
import com.qdlc.p2p.dal.dto.UserFreeze;
import com.qdlc.p2p.dal.model.UserFreezeModel;
import com.qdlc.p2p.dal.util.UserUtils;

@Repository("freezeDao")
public class UserFreezeDaoImpl extends BaseDaoImpl<UserFreeze> implements UserFreezeDao {

	@Override
	public PageDataList<UserFreezeModel> freezeList(int pageNumber, int pageSize, UserFreezeModel model) {
		QueryParam param = QueryParam.getInstance().addPage(pageNumber, pageSize);
		if(!StringUtil.isBlank(model.getSearchName())){//模糊查询
			param.addParam("user.userName", Operators.LIKE, model.getSearchName().trim());
		}else{//精确查询
			if (model.getStatus() != 99) {
				param.addParam("status", model.getStatus());
			}
			if (!StringUtil.isBlank(model.getUserName())) {
				param.addParam("user.userName", Operators.LIKE, model.getUserName().trim());
			}
		}
		param.addOrder(OrderType.DESC, "id");
		PageDataList<UserFreeze> pageDataList = super.findPageList(param);
		PageDataList<UserFreezeModel> pageDataList_ = new PageDataList<UserFreezeModel>();
		List<UserFreezeModel> list = new ArrayList<UserFreezeModel>();
		pageDataList_.setPage(pageDataList.getPage());
		if (pageDataList.getList().size() > 0) {
			for (int i = 0; i < pageDataList.getList().size(); i++) {
				UserFreeze freeze = (UserFreeze) pageDataList.getList().get(i);
				UserFreezeModel freezeModel = UserFreezeModel.instance(freeze);
				User freezeUser = UserUtils.getUserById(freeze.getUserId());
				freezeModel.setUserName(freezeUser.getUserName());
				list.add(freezeModel);
			}
		}
		pageDataList_.setList(list);
		return pageDataList_;
	}

	@Override
	public boolean isExistsByUserName(String userName) {
		String sql = "select count(1) from " + Global.DB_PREFIX + "user_freeze f, " + Global.DB_PREFIX + "user u where f.user_id = u.user_id and u.user_name = :userName";
		Query query = em.createNativeQuery(sql);
		query.setParameter("userName", userName);
		BigInteger count = (BigInteger) query.getSingleResult();
		return count.intValue() == 0 ? false : true;
	}
	
	@Override
	@CachePut(value="userFreeze", key="#userFreeze.userId")
	public UserFreeze save(UserFreeze userFreeze) {
		return super.save(userFreeze);
	}

	@Override
	public void freezeDelete(long id, int status) {
		String jpql = "UPDATE Freeze SET status = :status WHERE id = :id";
		Query query = em.createQuery(jpql);
		query.setParameter("status", status);
		query.setParameter("id", id);
		int result = query.executeUpdate();
		if (result != 1) {
			throw new UserException("更新失败！");
		}
	}

	@Override
	@Cacheable(value="userFreeze", key="#userId")
	public UserFreeze getByUserId(long userId) {
		QueryParam param = QueryParam.getInstance();
		param.addParam("userId", userId);
		return super.findByCriteriaForUnique(param);
	}

}
